import { Button, Modal } from 'antd';
import React, { useCallback } from 'react';

import { useRequest } from 'ahooks';

const ModalConfirmButton = ({
  message,
  text,
  service,
  buttonProps,
  onSuccess,
}) => {
  const { run, loading } = useRequest(service, { manual: true, onSuccess });

  const handleClick = useCallback(() => {
    Modal.confirm({
      title: '警告',
      content: message,
      type: 'warning',
      okText: '确定',
      cancelText: '取消',
      closable: true,
      onOk: () => {
        return run();
      },
    });
  }, [message, run]);
  return (
    <Button {...buttonProps} loading={loading} onClick={handleClick}>
      {text}
    </Button>
  );
};

export default React.memo(ModalConfirmButton);
